package com.jsh.erp.utils;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.tool.xml.Pipeline;
import com.itextpdf.tool.xml.XMLWorker;
import com.itextpdf.tool.xml.XMLWorkerHelper;
import com.itextpdf.tool.xml.css.CssFilesImpl;
import com.itextpdf.tool.xml.css.StyleAttrCSSResolver;
import com.itextpdf.tool.xml.html.CssAppliersImpl;
import com.itextpdf.tool.xml.html.HTML;
import com.itextpdf.tool.xml.html.TagProcessorFactory;
import com.itextpdf.tool.xml.html.Tags;
import com.itextpdf.tool.xml.parser.XMLParser;
import com.itextpdf.tool.xml.pipeline.css.CssResolverPipeline;
import com.itextpdf.tool.xml.pipeline.end.PdfWriterPipeline;
import com.itextpdf.tool.xml.pipeline.html.HtmlPipeline;
import com.itextpdf.tool.xml.pipeline.html.HtmlPipelineContext;

import java.io.*;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;

public class PrintUtils {

	public static void printToPDF(Rectangle pagesize, String htmlContent, OutputStream outputStream)
			throws DocumentException, UnsupportedEncodingException, IOException {

		InputStream inputStream = new ByteArrayInputStream(htmlContent.getBytes("UTF-8"));
		PrintUtils.printToPDF(pagesize, inputStream, outputStream);
	}

	public static void printToPDF(Rectangle pagesize, InputStream inputStream, OutputStream outputStream)
			throws DocumentException, UnsupportedEncodingException, IOException {
		Document document = new Document(pagesize, 3, 3, 3, 3);
		PdfWriter pdfWriter = PdfWriter.getInstance(document, outputStream);
		document.open();
		document.addCreationDate();
		final TagProcessorFactory tagProcessorFactory = Tags.getHtmlTagProcessorFactory();
		tagProcessorFactory.removeProcessor(HTML.Tag.IMG);
		tagProcessorFactory.addProcessor(new ImageTagProcessor(), HTML.Tag.IMG);

		final CssFilesImpl cssFiles = new CssFilesImpl();
		cssFiles.add(XMLWorkerHelper.getInstance().getDefaultCSS());
		final StyleAttrCSSResolver cssResolver = new StyleAttrCSSResolver(cssFiles);
		final HtmlPipelineContext hpc = new HtmlPipelineContext(new CssAppliersImpl(new AsianFontProvider()));
		hpc.setAcceptUnknown(true).autoBookmark(true).setTagFactory(tagProcessorFactory);
		final HtmlPipeline htmlPipeline = new HtmlPipeline(hpc, new PdfWriterPipeline(document, pdfWriter));
		final Pipeline<?> pipeline = new CssResolverPipeline(cssResolver, htmlPipeline);

		final XMLWorker worker = new XMLWorker(pipeline, true);
		final Charset charset = Charset.forName("UTF-8");
		final XMLParser xmlParser = new XMLParser(true, worker, charset);

		xmlParser.parse(inputStream, StandardCharsets.UTF_8);
		document.close();
	}

//
//	public static void printToPDF(String htmlContent, OutputStream outputStream) throws Exception {
//		PdfRendererBuilder builder = new PdfRendererBuilder();
//		builder.useDefaultPageSize(50,40, BaseRendererBuilder.PageSizeUnits.MM);
//		ClassLoader classLoader = PrintUtils.class.getClassLoader();
////		builder.useFont(new File(classLoader.getResource("fonts/microsoft-yahei/Microsoft-YaHei-Light.ttc").getPath()),"Microsoft Yahei");
////		builder.withUri("file:///Users/admin/Desktop/test.html");
//		builder.withHtmlContent(htmlContent,null);
//		builder.useFastMode();
//		builder.toStream(outputStream);
//		builder.useSVGDrawer(new BatikSVGDrawer()); // Load SVG support plugin
//		builder.run();
//	}
//
//    public static void main(String[] args) {
//        try (OutputStream os = new FileOutputStream("C:/Users/admin/Desktop/test.pdf")) {
//            PdfRendererBuilder builder = new PdfRendererBuilder();
//			builder.useDefaultPageSize(50,40, BaseRendererBuilder.PageSizeUnits.MM);
//            builder.useFont(new File("C:/Users/admin/Desktop/MSYH.TTC"),"Microsoft Yahei");
//            builder.withUri("file:///Users/admin/Desktop/test.html");
//            builder.useFastMode();
//            builder.toStream(os);
//            builder.useSVGDrawer(new BatikSVGDrawer()); // Load SVG support plugin
//            builder.run();
//        } catch (FileNotFoundException e) {
//			e.printStackTrace();
//		} catch (IOException e) {
//			e.printStackTrace();
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
//	}
}
